package FP 
{
	/**
	 * ...
	 * @author Ty Kimmey
	 */
	
	 import flash.display.Sprite;
	 import flash.events.Event;
	 import com.as3toolkit.events.KeyboarderEvent;
	 import com.as3toolkit.ui.Keyboarder;
	 import FP.GameElement;
	 import FP.screen.Screens;
	 
	 
	public class Game extends Sprite
	{
		private var levelXML:XML;
		private var myRhino:Rhino;
		private var myHUD:HUD;
		private var over:Boolean = false;
		private var myObjectManager:ObjectManager;
		private var currentlevel:int = -1;
		private var myRoad:Road;
		private var time:int = 100;
		public var aLayer:Sprite;
		public var bLayer:Sprite;
		public var pLayer:Sprite;
		public var gTimer:int;
		public var screenI:Screens;
		
		public function Game(lvlXML:XML) 
		{
			super();
			trace("Hello, game!");
			levelXML = lvlXML;
			myHUD = new HUD(lvlXML.hud[0]);			
		}
		
		public function begin()
		{
			screenI = new Screens();
			aLayer = new Sprite();
			bLayer = new Sprite();
			pLayer = new Sprite();
			
			addChild(aLayer);
			addChild(bLayer);
			addChild(pLayer);
			addChild(myHUD);
			
			myRoad = new Road(this);
			myRhino = new Rhino(this);
			aLayer.addChild(myRoad);
			pLayer.addChild(myRhino);
			myRhino.init();
			
			nextLevel();
			
			//myObjectManager = new ObjectManager(this, levelXML, 0); //need to fix
			
			addEventListener(Event.ENTER_FRAME, update);
		}
						
		public function update(e:Event)
		{
			myRhino.move();
			myObjectManager.updateObject();
			
			time--;
			if (time <= 0)
			{
				trace("Time");
				myObjectManager.spawnObject();
				time = 100;
				gTimer = 100;
			}
			
			var n:Nerd;
			var b:Baby;
			var w:Wall;

			for (var i:int = 0; i < myObjectManager.activeNerds.length; i++)
			{
				n = (myObjectManager.activeNerds[i] as Nerd); 
				
				if (myRhino.hitTestObject(n))
				{
					//myHUD.score_txt.text = "Score: " + score; // method
					myHUD.addPoints(30);
					myObjectManager.activeNerds.splice(i, 1);
					bLayer.removeChild(n);		
				}	
			}

			for (var j:int = 0; j < myObjectManager.activeBabies.length; j++)
			{
				b = (myObjectManager.activeBabies[j] as Baby);
				
				if (myRhino.hitTestObject(b))
				{
					//myHUD.score_txt.text = "Score: " - score; // method
					myHUD.addPoints( -15);
					myObjectManager.activeBabies.splice(j, 1);
					bLayer.removeChild(b);
				}
			}

			for (var k:int = 0; k < myObjectManager.activeWalls.length; k++)
			{
				w = (myObjectManager.activeWalls[k] as Wall);
				
				if (myRhino.hitTestObject(w))
				{
					//myHUD.lives_txt.text = "Lives: " - lives; // method
					myHUD.takeLives(1);
					myObjectManager.activeWalls.splice(k, 1);
					bLayer.removeChild(w);
				}
			}
			
			if (myObjectManager.pooledNerds.length == 0 && over == false)
			{
				nextLevel();
			}
			
			if (myHUD.lives <= 0)
			{
				over = true;
				myHUD.message_txt.text = "Game Over";
			}			
			if (over == true)
			{
				//(parent as Document).gotoAndPlay("Credits");
				//removeEventListener(Event.ENTER_FRAME, update);
				/*gTimer--;
				if (gTimer <= 0)
				{
					(parent as Document).gotoAndStop("Credits");
				}
				//(parent as Document).gotoAndStop("Credits");*/
			}
		}
		
		public function nextLevel()
		{
			currentlevel++;
			
			if (currentlevel < (levelXML.levels[0].level as XMLList).length())
			{
				myHUD.level_txt.text = "Level: " + currentlevel;
				myObjectManager = new ObjectManager(this, levelXML, currentlevel);				
			}
			else 
			{
				over = true;
				myHUD.message_txt.text = "You win!";
			}
		}
		
		public function gameOver():void
		{
			myRhino.visible = false;
			myHUD.message_txt.text = "Game Over";
			over = true;
		}
		
		public function spawnWalls(obj:Wall)
		{
			obj = new Wall(this);
			bLayer.addChild(obj);
			obj.y = -10; obj.x = (Math.random() * stage.stageWidth);
			myObjectManager.activeWalls.push(obj);
			
		}
		
		public function spawnNerds(obj:Nerd)
		{
			obj = new Nerd(this);
			bLayer.addChild(obj);
			obj.y = -10; obj.x = (Math.random() * stage.stageWidth);
			myObjectManager.activeNerds.push(obj);
		}
		
		public function spawnBabies(obj:Baby)
		{
			obj = new Baby(this);
			bLayer.addChild(obj);
			obj.y = -10; obj.x = (Math.random() * stage.stageWidth);
			myObjectManager.activeBabies.push(obj);
		}
		
	}

}